package beans;

import javax.annotation.Resource;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.jms.*;

/**
 * Session Bean implementation class Prima
 */
@Stateless
@LocalBean
public class Prima implements PrimaRemote {

	@Resource(mappedName = "jms/Firstjms")
	private ConnectionFactory connectionFactory;
	
	@Resource(mappedName = "jms/Firstjmsqueue")
	private Queue queue;
	
	MessageProducer messageProducer;
	TextMessage textMessage;
	Session session;
	Connection connection;
	
    /**
     * Default constructor. 
     */
    public Prima() {
        // TODO Auto-generated constructor stub
    }
    
   
	public void salut() {
    	try{
    		connection = connectionFactory.createConnection();
    		
    		session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
    		
    		messageProducer = session.createProducer(queue);
    		textMessage = session.createTextMessage();
    		textMessage.setText("mesajul 1");
    		messageProducer.send(textMessage);
    		textMessage.setText("mesajul 2");
    		messageProducer.send(textMessage);
    		textMessage.setText("KTHXBYE!");
    		messageProducer.send(textMessage);
    		
    		messageProducer.close();
    		session.close();
    		connection.close();
   	
    	}
    	catch (JMSException e) {
    		e.printStackTrace();
    	}
    }

}
